﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.Controller
{
    public partial class MarketComparisonsSelector : System.Web.UI.UserControl
    {
        DataControllerDataContext dcdc = new DataControllerDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var users = from u in dcdc.aspnet_Users
                            where u.UserName == Page.User.Identity.Name
                            select u;
                Guid agentId = new Guid();
                foreach (var user in users)
                {
                    agentId = user.UserId;
                    break;
                }


                int agentPlaceId = 42;
                var placeIds = from p in dcdc.place_agent_tables
                               where p.UserID == agentId
                               select p;

                foreach (var placeId in placeIds)
                {
                    agentPlaceId = placeId.suburbid.Value;
                }
                var markComparisonsList = from mc in dcdc.market_comparison_property_tables
                                          where mc.suburbid == agentPlaceId
                                          select new { title = mc.address + "   $" + mc.price, mc.propertyID };
                ListItem li;
                DropDownList1.Items.Clear();


                li = new ListItem();
                li.Text = "Please Select";
                li.Value = "1";
                li.Selected = true;
                DropDownList1.Items.Add(li);


                int i = 0;
                foreach (var markComparison in markComparisonsList)
                {
                    li = new ListItem();
                    li.Text = markComparison.title;
                    li.Value = markComparison.propertyID;
                    DropDownList1.Items.Add(li);
                    i++;
                }

                if (i == 0)
                {
                    li = new ListItem();
                    li.Text = "No more";
                    li.Value = "1";
                    DropDownList1.Items.Add(li);
                }
            }
        }

        public String getSelectedValue()
        {
            return DropDownList1.SelectedItem.Value;
        }
    }
}